package com.mingtang.online.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.mingtang.online.R;
import com.mingtang.online.changliang.Const;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/10/16.
 */

public class TaoKoulingActivity extends BaseActivity {

    @BindView(R.id.v4_webview)
    WebView v4Webview;
    String itemid;


    WebChromeClient chromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView webView, int i) {
            Log.e("进度", i + "");
            if (i == 100) {
                dismissLoading();
            } else {
                showLoading();
            }
        }
    };
    String url;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hongbao);
        ButterKnife.bind(this);
        itemid = getIntent().getStringExtra("itemid");
        initWebView();
//        http://www.50210.com.cn/app/index.php?i=3&c=entry&do=view&m=tiger_newhu&lm=0&itemid=547360352897&pid=mm_120991317_38116267_139518914&dluid=
//        url = Const.MODEL_URL + "i=3&c=entry&do=view&m=tiger_newhu&lm=0&itemid=" + itemid + "&pid=mm_120991317_38116267_139518914&dluid=";
        url = Const.MODEL_URL + getIntent().getStringExtra("url");
        v4Webview.loadUrl(url);
    }

    public void initWebView() {
        v4Webview.clearCache(true);

//        v4Webview.setWebChromeClient(chromeClient);
        v4Webview.setWebChromeClient(chromeClient);
        v4Webview.getSettings().setUseWideViewPort(true);

        v4Webview.getSettings().setLoadWithOverviewMode(true);
        v4Webview.getSettings().setSavePassword(true);
        v4Webview.getSettings().setSaveFormData(true);
        v4Webview.getSettings().setJavaScriptEnabled(true);

        // enable navigator.geolocation
        v4Webview.getSettings().setGeolocationEnabled(true);
        v4Webview.getSettings().setGeolocationDatabasePath(
                "/data/data/org.itri.html5webview/databases/");

        v4Webview.requestFocus();
        v4Webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        // v4_webview.setScrollBarStyle(0);
        String ua = v4Webview.getSettings().getUserAgentString();
        v4Webview.getSettings().setUserAgentString(
                ua + "; " + "mingtang_android");
        v4Webview.addJavascriptInterface(new TaoKoulingActivity.JsInterfaces(this),
                "AndroidWebView");
        v4Webview.getSettings().setBuiltInZoomControls(true);
        v4Webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        v4Webview.getSettings().setDomStorageEnabled(true);

        v4Webview.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);

        v4Webview.getSettings().setAllowFileAccess(true);
        v4Webview.getSettings().setAppCacheEnabled(true);
        v4Webview.getSettings().setJavaScriptEnabled(true);
//        v4Webview.getSettings().setAppCachePath(appCachePath);
        v4Webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        v4Webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
//		v4_webview.getSettings().setSupportZoom(true);
        v4Webview.getSettings().setDefaultTextEncodingName("utf-8");
        v4Webview.getSettings().setSupportZoom(false);
        v4Webview.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent ev) {

                ((android.webkit.WebView) v).requestDisallowInterceptTouchEvent(true);

                return false;
            }
        });
     /*   v4Webview.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(android.webkit.WebView view, String url) {


                return false;
            }


        });*/
    }


    public class JsInterfaces {
        private Context mContext;


        public JsInterfaces(Context mContext) {
            super();
            this.mContext = mContext;

        }


        @JavascriptInterface
        public void finishActivity() {
//            finish();
//            Toast.makeText(ShopDetailActivity.this, "1234", Toast.LENGTH_SHORT).show();
            TaoKoulingActivity.this.finish();
        }

        //同款
        @JavascriptInterface
        public void sameparagraph(String sameurl) {
//            finish();
            Toast.makeText(TaoKoulingActivity.this, "同款", Toast.LENGTH_SHORT).show();
            Log.d(sameurl, "44444444444444444");
        }

        //首页
        @JavascriptInterface
        public void homeindex() {
//            finish();
            Toast.makeText(TaoKoulingActivity.this, "首页", Toast.LENGTH_SHORT).show();

        }
//掏口令

        @JavascriptInterface
        public void taoKouLing(String taourl) {
//            finish();
            Toast.makeText(TaoKoulingActivity.this, "掏口令", Toast.LENGTH_SHORT).show();
            Log.d(taourl, "2222222222222222222222222");
        }


    }
}
